//
//  StuOrTeaViewController.m
//  test
//
//  Created by 朱圳 on 2018/1/5.
//  Copyright © 2018年 朱圳. All rights reserved.
//

#import "GRStuOrTeaViewController.h"
#import "OderHeadCell.h"
#import "StuOrderCell.h"
#import "OrderModel.h"
@interface GRStuOrTeaViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)NSMutableArray <OrderModel*>*stuArray;
@property(nonatomic,strong)UITableView *tableView;
@end

@implementation GRStuOrTeaViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"我约学生";
    [self.view addSubview:self.tableView];
    self.view.backgroundColor = [UIColor whiteColor];
    [self requestData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)requestData{
        [EasyShowLodingView showLodingText:@""];
    __weak typeof(self) weakSelf = self;
    NSDictionary *dict = @{
                           @"gid" : Gid,
                           @"gpass" : Gpassword,
                           };
    [[AFNetworkTool shareManager]postJSONWithUrl:GR_Order_list parmas:dict successData:^(id json) {
        NSArray *arr = json[@"data"];
        for (int i = 0; i<arr.count; i++) {
            OrderModel *model = [OrderModel yy_modelWithJSON:arr[i]];
            
            [weakSelf.stuArray addObject:model];
        }
        self.stuArray = (NSMutableArray *)[[self.stuArray reverseObjectEnumerator] allObjects];
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [EasyShowLodingView hidenLoding];
            if (self.stuArray.count==0) {
                [self.tableView removeFromSuperview];
                KongDataView *konview=[KongDataView NilDataview];
                konview.frame=self.view.frame;
                [self.view addSubview:konview];
            }
        });
        dispatch_async(dispatch_get_main_queue(), ^{
            [weakSelf.tableView reloadData];
        });
        //  [Utility showFail:@"成功"];
    } failure:^(NSError *error) {
        NSLog(@"%@",error);
        [Utility showFail:@"网络错误"];
        [EasyShowLodingView hidenLoding];
    }];
}
#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.stuArray.count;
    
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    StuOrderCell *cell = [tableView dequeueReusableCellWithIdentifier:@"stuOrderCell"];
    if (!cell) {
        cell = [[StuOrderCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"stuOrderCell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    cell.model = self.stuArray[indexPath.row];
    cell.nameLab.text = cell.model.sname;
    return cell;
}
/** 区头视图 */
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (self.stuArray.count>0) {
        OderHeadCell *header = [[OderHeadCell alloc] initWithReuseIdentifier:@"header"];
        header.operationLab.text = @"状态";
        return header;
    }else{
        return nil;
    }
}
#pragma mark - 懒加载
-(NSMutableArray *)stuArray{
    if (!_stuArray) {
        _stuArray = [[NSMutableArray alloc]init];
    }
    return _stuArray;
}
-(UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)style:UITableViewStyleGrouped];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerClass:[StuOrderCell class] forCellReuseIdentifier:@"stuOrderCell"];
    }
    return _tableView;
}
@end
